﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Net;
using System.IO;
using System.Net.Sockets;
using System.Threading;
using System.Windows.Forms;

namespace TestEDS
{
    public partial class Form1 : Form
    {

        private ManualResetEvent Done = new ManualResetEvent(false);

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback(Listen));          
        }
        private void Listen(object state)
        {
            Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            sock.Bind(
                new IPEndPoint(
                    IPAddress.Any, 8080)
                    );
            sock.Listen(1024);
            while (true)
            {
                Done.Reset();
                sock.BeginAccept(new AsyncCallback(Connected), sock);
                Done.WaitOne();
            }
        }
        private void Connected(IAsyncResult Result)
        {
            Socket sock = (Socket)Result.AsyncState;
            Socket handle = sock.EndAccept(Result);
            textBox1.Invoke(new EventHandler(settext), "获取到一个连接\r\n");
            StateObject State = new StateObject(handle);
            handle.BeginReceive(State.buffer, 0, 16, 0, new AsyncCallback(Read), State);


        }

        private void Read(IAsyncResult Ar)
        {
            StateObject State = (StateObject)Ar.AsyncState;
            Socket sock = State.WorkSocket;
            int rc = sock.EndReceive(Ar);
            if (rc >0)
            {
                State.dataStream.Write(State.buffer, 0, rc);
                State.dataStream.Flush();
                if (rc < 16)
                {
                    sock.Close();
                    string txtdata = Encoding.UTF8.GetString(State.dataStream.ToArray());
                    textBox1.Invoke(new EventHandler(settext), txtdata);
                    return;
                }
                sock.BeginReceive(State.buffer, 0, 16, 0, new AsyncCallback(Read), State);
            }
            else
            {
                sock.Close();
                StreamReader sw = new StreamReader(State.dataStream);
                string txtdata = sw.ReadToEnd();
                textBox1.Invoke(new EventHandler(settext), txtdata);
                return;
            }

        }


        private void settext(object txt,EventArgs args)
        {
            textBox1.Text = txt.ToString();
        }
    }
    class StateObject
    {
        public Byte[] buffer { get; set; }
        public Socket WorkSocket { get; set; }
        public MemoryStream dataStream { get; set; }

        public StateObject(Socket workSock)
        {
            buffer = new byte[16];
            WorkSocket = workSock;
            dataStream = new MemoryStream();
        }

    }
}
